﻿define([
  'models/abmManagers/preferenceManager',
  'text!templates/abmManagers/abmBaseTmp.html',
  'text!templates/abmManagers/abmTableTmp.html',
  'text!templates/abmManagers/preference/super/preferenceManager.html',
], function (prefModel, BaseTmp, TableTmp, UniqTmp) {
    var view = Backbone.View.extend({
        el: $("#main"),
        _uId: "",
        render: function () {
            //$("#mainLoading").show();
            toggleLoading(true);
            $("div[data-rel='abm-preference']").remove();
            var prefMod = new prefModel();
            var _this = this;
            this._uId = $.uuid();

            prefMod.List(undefined, function (dataPref) {
                var baseTemplate = Handlebars.compile(BaseTmp);
                var tableTemplate = Handlebars.compile(TableTmp);
                var uniqTemplate = Handlebars.compile(UniqTmp);

                var dataBaseTemplate = {
                    id: _this._uId,
                    dataRel: "abm-preference", // cuidado, ref en js
                    tmpClass: "preference-manager", // cuidado, ref en css
                    title: translate("_preferenceManager")
                };
                var dataTableTemplate = {
                    headers: [translate("_code"), translate("_value"), translate("_type"),
                         translate("_description"), translate("_options")]
                };
                var dataUniqTemplate = {
                    data: dataPref
                };

                var html = baseTemplate(dataBaseTemplate);
                var html2 = tableTemplate(dataTableTemplate);
                var html3 = uniqTemplate(dataUniqTemplate);

                _this.$el.append(html);
                $("div[data-id='" + _this._uId + "']").find("div.abm-content").append(html2);
                $("div[data-id='" + _this._uId + "']").find("tbody").append(html3);

                _this.addTableHandlers($("div[data-id='" + _this._uId + "']").find("table"));
                $("div[data-id='" + _this._uId + "']").find("table").cubeTable();
                
                _this.addHandlers();
                //$("#mainLoading").hide();
                toggleLoading(false);
            });
        },
        edit: function (e) {
            var preferenceId = $(e.currentTarget).closest("tr").attr("data-id");

            require(['controllers/abmManagers/preferenceManager'], function (Controller) {
                (new Controller()).showEdit(preferenceId);
            });

        },
        close: function () {
            var key = $("#currentTask").attr("data-rel");
            require(['controllers/home/home'], function (Controller) {
                (new Controller()).removeTask(key);
            });
        },
        addTableHandlers: function (oTable) {
            $(oTable).find("button.btn-edit").on("click", this.edit);
        },
        addHandlers: function () {
            $("div[data-id='" + this._uId + "']").find("div.abm-top-options a.btn-close").on("click", this.close);
        }
    });
    return view;
});